package linker.list;

import javax.swing.JOptionPane;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;

import linker.account.Account;

/**
 * 
 * @version linker 0.93
 * @author Jianfeng jianfeng.linker@gmail.com
 * 
 */
public class SubscribeRequest extends DefaultListItem {

	private String user;

	private XMPPConnection connection;

	public SubscribeRequest(String user) {
		this.user = user;

		connection = Account.getConnection();
	}

	public String toString() {
		return "A subcribeRequest";
	}

	public void doubleClick() {
		Account.getMessageList().removeMessage(this);
		int i = JOptionPane.showConfirmDialog(null,
				user + " ask for subscribe", "Subscribe Request",
				JOptionPane.YES_NO_OPTION);
		if (i == JOptionPane.OK_OPTION) {
			Presence presence = new Presence(Presence.Type.subscribe);
			presence.setTo(user);
			presence.setFrom(connection.getUser());
			connection.sendPacket(presence);
		} else {
			Presence presence = new Presence(Presence.Type.unsubscribe);
			presence.setTo(user);
			presence.setFrom(connection.getUser());
			connection.sendPacket(presence);
		}

		Account.removeMessage(this);
	}

}
